package com.woniu.controller;

import com.alibaba.druid.sql.visitor.functions.Substring;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.woniu.entity.CarManager;
import com.woniu.entity.Client;
import com.woniu.entity.OverdueManagement;
import com.woniu.entity.SecondTrial;
import com.woniu.service.OverdueManagementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


@Api(value = "订单逾期信息模块", tags = {"逾期管理"})
@RestController
@RequestMapping("/overdueManagement")
public class OverdueManagementController extends BaseController {
    @Autowired
    OverdueManagementService overdueManagementService;

    @ApiOperation(value = "查询所有逾期订单", response= OverdueManagement.class,  httpMethod="GET", tags = {"逾期管理"})
    @ApiImplicitParam(name = "overdueManagement",value = "逾期实体",required = true)
    @GetMapping("/findAll")
    public TableDataInfo findAll(OverdueManagement overdueManagement) {
        startPage();
        List<OverdueManagement> all = overdueManagementService.findAll(overdueManagement);
        return getDataTable(all);
    }


    @ApiOperation(value = "导出逾期订单信息", response= Integer.class, httpMethod="POST", tags = {"逾期管理"})
    @PreAuthorize("@ss.hasPermi('test:export')")
    @Log(title = "逾期管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OverdueManagement overdueManagement) throws IOException {
        List<OverdueManagement> list = overdueManagementService.exportOverdueList();
        ExcelUtil<OverdueManagement> util = new ExcelUtil<>(OverdueManagement.class);
        util.exportExcel(response,list,"逾期信息数据");
    }
    @ApiOperation(value = "选中指定逾期订单进行催收", response= OverdueManagement.class, httpMethod="POST", tags = {"逾期管理"})
    @PostMapping("/updateOverdue")
    public void updateOverdueManagement(@RequestBody String str){
        JSONObject jsonObject=JSONObject.parseObject(str);
        String ids =jsonObject.getString("ids");
        String substring =ids.substring(1,ids.length() - 1);
        String[] strs=substring.split(",");
        int array[]=new int[strs.length];
        for(int i=0;i<strs.length;i++){
            array[i]=Integer.parseInt(strs[i]);
        }
        overdueManagementService.updateOverdueManagement(array);
    }
}
